package com.example.vv.frameWork.until;

import android.app.Activity;
import android.content.SharedPreferences;

import com.example.vv.frameWork.entity.LoginEntity;
import com.example.vv.frameWork.sp.Userinfo;

public class SharePUntitl {
    public static  Userinfo userinfo;
    public static boolean isLogin;
    public static SharedPreferences init(Activity activity) {
        SharedPreferences vv = activity.getSharedPreferences("Vv", activity.MODE_PRIVATE);
        return vv;
    }

    public static Userinfo getUserBean(Activity activity) {
        if(userinfo!=null){
            isLogin=true;
            return userinfo;
        }
        SharedPreferences sharedPreferences = init(activity);
        String phone = sharedPreferences.getString("phone", null);
        String password = sharedPreferences.getString("password", null);
        String imageUrl = sharedPreferences.getString("imageUrl", null);
        String name = sharedPreferences.getString("name", null);
        if (phone == null) {
            isLogin=false;
            return null;
        } else {
             userinfo = new Userinfo();
            userinfo.setPhone(phone);
            userinfo.setPassWord(password);
            userinfo.setName(imageUrl);
            userinfo.setName(name);
            isLogin=true;
            return userinfo;
        }
    }

    public static void addUserInfo(Activity activity, LoginEntity loginEntity) {
        init(activity)
                .edit()
                .putString("phone", loginEntity.getUserBean().getPhone())
                .putString("password", loginEntity.getUserBean().getPassword())
                .putString("imageUrl", loginEntity.getUserBean().getPic())
                .putString("name", loginEntity.getUserBean().getName())
                .commit();
        isLogin=true;
    }

    public static void clearUserInfo(Activity activity) {
        init(activity)
                .edit()
                .clear()
                .commit();
        isLogin=false;
    }

}
